package sjtu.ist.warehouse4ws.action;

import java.util.List;

import sjtu.ist.warehouse4ws.model.Company;
import sjtu.ist.warehouse4ws.model.User;
import sjtu.ist.warehouse4ws.service.CompanyService;
import sjtu.ist.warehouse4ws.service.PrivilegeService;
import sjtu.ist.warehouse4ws.service.UserService;

import com.googlecode.jsonplugin.annotations.JSON;
import com.opensymphony.xwork2.ActionSupport;

public class RegistAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private UserService userService;
	private CompanyService companyService;
	private PrivilegeService privilegeService;
	private List<User> users;
	private User user;
	private Company company;

	private boolean success;
	private String message;
	private String userName;
	private String pw1;
	private String pw2;
	private String companyName;
	private String userId;
	
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	public void setCompanyService(CompanyService companyService){
		this.companyService = companyService;
	}

	public void setPrivilegeService(PrivilegeService privilegeService) {
		this.privilegeService = privilegeService;
	}

//	@JSON(name = "isUserNameRegisted")
//	public boolean isUserNameRegisted() {
//		return userService.isUserNameRegisted(userName, pw1);
//	}
//
//	@JSON(name = "isCompanyNameRegisted")
//	public boolean isCompanyRegisted() {
//		return userService.isCompanyNameRegisted(userName, pw1,
//				companyName);
//	}
//	


	public void regist() {
		user = new User();
		company = new Company();
//		System.out.println(companyName);
//		System.out.println(pw1);
//		System.out.println(userName);
		String companyId = java.util.UUID.randomUUID().toString();
		company.setId(companyId);
		company.setName(companyName);
		companyService.save(company);
		
		privilegeService.addPrivilegeForCompany(companyId);
		
		String userId = java.util.UUID.randomUUID().toString();
		user.setId(userId);
		user.setCompany(companyName);
		user.setName(userName);
		user.setPassword(pw1);
		user.setType(1);
		userService.save(user);

	}

	public String execute() throws Exception {
		String str1="用户名已经被注册，请重新选择用户名";
		String str2="公司名已经被注册，请联系管理员";
		String str3="用户注册成功";
		success=true;
		if(userService.isUserNameRegisted(getUserName(), getPw1())){
			message = str1;
			return SUCCESS;
		}
		if(userService.isCompanyNameRegisted(getUserName(),getPw1(),getCompanyName())){
			message = str2;
			return SUCCESS;
		}
		regist();
//	    success=true;
		message = str3;
		return SUCCESS;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getUserName() {
		return userName;
	}

	

	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}

	public String getCompanyName() {
		return companyName;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	@JSON(name = "message")
	public String getMessage() {
		return message;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getUserId() {
		return userId;
	}

	public void setPw1(String pw1) {
		this.pw1 = pw1;
	}

	public String getPw1() {
		return pw1;
	}

	public void setPw2(String pw2) {
		this.pw2 = pw2;
	}

	public String getPw2() {
		return pw2;
	}

}
